<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
 <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container">
		<button type="button" class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">新增计划</button>
	</div>
</script>

<script type="text/html" id="currentTableBar">
     <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">查看详情</a>
     <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="add">新增计划项</a>
     <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="update">修改</a>
     <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
</script>
<!-- <script type="text/html" id="currentTableBar2">
     <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="update">修改</a>
 </script>-->
 <script type="text/html" id="toolbarDemo2">
     <div class="layui-btn-container">
         <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
     </div>
 </script>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var util = layui.util;
        var $ = layui.jquery,
        form = layui.form,
        table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/purchaseplan',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'pupId', width: 200, title: '采购编号' },
                {field: 'purchaseDate', width: 180, title: '采购计划创建日期',sort: true},
                {field: 'planstaff', width: 130, title: '计划执行人',templet: '<div>{{d.emp.empName}}</div>'},
                {field: 'status', title: '计划状态', Width: 100},
                {field: 'remark', width: 300, title: '详情'},
                {title: '操作', minWidth: 280, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
        });
        table.on('toolbar(currentTableFilter)', function (obj) {
             // 监听添加操作
            if (obj.event === 'add') {
                var index = layer.open({
                    title: '新增计划',
                    type: 2,//默认不动
                    shade: 0.2,//默认不动
                    maxmin: true,//默认不动
                    shadeClose: true,//默认不动
                    area: ['70%', '90%'],
                    content: '../plan/add-purchaseplan.html',
                });
                /*$(window).on("resize", function () {
                    layer.full(index);
                });*/
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if(obj.event === 'edit'){
                var index = layer.open({
                    type: 1,
                    content: " <table class=\"layui-hide\" id=\"OrderUnitTableId2\" lay-filter=\"currentTableFilter2\"></table>",
                    offset: ['150px', '60px']
                });
            }else if (obj.event === 'add') {
                if (data.status == "已通过"){
                    layer.open({
                        title:['确认消息'],
                        content: "计划已通过，无法添加",
                        btn: ['确定'],
                        shadeClose: true,
                        yes: function(index, layero){
                            // 关闭弹出层
                            layer.close(index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                            table.reload('OrderUnitTableId2');
                        }
                    })
                }else if (data.status == "未审核") {
                    var index = layer.open({
                        title: '新增计划项',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['70%', '60%'],
                        content: '../plan/add-purchaseplangoods.html?pupId='+data.pupId,
                    });
                }

            }else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    if (data.status == "已通过"){
                        layer.open({
                            title:['确认消息'],
                            content: "计划已通过，无法删除",
                            btn: ['确定'],
                            shadeClose: true,
                            yes: function(index, layero){
                                // 关闭弹出层
                                layer.close(index);
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                table.reload('OrderUnitTableId2');
                            }
                        })
                    }else if (data.status == "未审核") {
                        $.post('/deletepurchaseplan',
                            {
                                pupId:data.pupId
                            },
                            function(result){
                                var index = layer.alert(result.msg, {
                                    title: '提示信息',

                                },function () {
                                    // 关闭弹出层
                                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(iframeIndex);
                                });if(result.code===0){
                                    obj.del();
                                    layer.close(index);
                                }
                            })
                    }

                });
            }else if (obj.event === 'update'){
                if (data.status == "已通过"){
                    layer.open({
                        title:['确认消息'],
                        content: "计划已通过，无法修改",
                        btn: ['确定'],
                        shadeClose: true,
                        yes: function(index, layero){
                            // 关闭弹出层
                            layer.close(index);
                            var iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                            table.reload('OrderUnitTableId2');
                        }
                    })
                }else if (data.status == "未审核") {
                    var index = layer.open({
                   title: '修改计划',
                   type: 2,
                   shade: 0.2,
                   maxmin:true,
                   shadeClose: true,
                   area: ['70%', '60%'],
                   content: '../plan/update-purchaseplan.html?pupId='+data.pupId,
               });
                }
               
            }


                table.render({
                    elem: '#OrderUnitTableId2',
                    url: '/purchasegoods?pupId='+data.pupId,
                    toolbar: '#toolbarDemo2',
                    defaultToolbar: ['filter', 'exports', 'print', {
                        title: '计划详情',
                        layEvent: 'LAYTABLE_TIPS',
                        icon: 'layui-icon-tips'
                    }],
                    cols: [[
                        {type: "checkbox", width: 50},
                        {field: 'prpId', width: 200, title: '计划编号',templet: '<div>{{d.purchaseplan.pupId}}</div>' },
                        {field: 'prdName', width: 180, title: '物料名称',templet: '<div>{{d.material.maename}}</div>'},
                        {field: 'model', width: 180, title: '物料型号', templet: '<div>{{d.material.model}}</div>'},
                        {field: 'texture', width: 180, title: '材质',templet: '<div>{{d.material.texture}}</div>'},
                        {field: 'unit', width: 180, title: '计件单位',templet: '<div>{{d.material.unit}}</div>'},
                        {field: 'quantity', title: '订单数量', minWidth: 150,sort: true},
                        {title: '操作', minWidth: 120, toolbar: '#currentTableBar2', align: "center"}
                    ]],
                    limits: [4, 8, 16, 24],
                    limit: 4,
                    page: true,

                    skin: 'line',

                });

            /*$(window).on("resize", function () {
                layer.full(index);
            });*/
            return false;
        });
        /*table.on('tool(currentTableFilter2)',function(obj){
            layer.alert(JSON.stringify(obj.data))
            if (obj.event === 'update'){
                var data = obj.data;
                var index = layer.open({
                    title: '修改计划项',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['70%', '60%'],
                    content: '../plan/update-purchasegoods.html?pugId=' + data.pugId,
                })
                $(window).on("resize", function () {
                    layer.full(index);
                });


            }
        });*/


        table.on('toolbar(currentTableFilter2)', function (obj) {

            if (obj.event === 'add') {  // 监听添加操作
                var checkStatus = table.checkStatus('OrderUnitTableId2'),
                    data = checkStatus.data;
                layer.alert(JSON.stringify(data));
                var index = layer.open({
                    title: '新增计划项',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['70%', '60%'],
                    content: '../plan/add-planunit.html?prpId='+data.prpId,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('OrderUnitTableId2'),
                    pugIds = new Array(),
                    data = checkStatus.data,
                    length = data.length;
                    for(var i=0;i<length;i++) {
                        pugIds[i] = data[i].pugId;//得到复选框的值
                    };
                $.ajax({
                    type:"post",
                    url:"/deletepurchaseplangoods",
                    data:{
                        pugIds:pugIds,
                    },
                    success:function(data){
                        var msg = data.msg;
                        layer.open({
                            title:['确认消息'],
                            content: msg,
                            btn: ['确定'],
                            shadeClose: true,
                            yes: function(index, layero){
                                // 关闭弹出层
                                layer.close(index);
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                                table.reload('OrderUnitTableId2');
                            }
                        })
                    }

                })

            }

        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            layer.alert(result, {
                title: '最终的搜索信息'
            });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });
    });
</script>

</body>
</html>